<!-- 公文类型 -->
<template>
  <div class="app-container">
    <div style="padding: 5px" />
    <el-button type="primary" size="small" @click="onclickAdd"
      >新增类型</el-button
    >

    <div style="padding: 5px" />
    <el-table :data="list" border row-key="id" align="left">
      <el-table-column label="标签名称" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="mini"
            plain
            @click="onClickChange(scope.row)"
          >
            修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="新增文件标签"
      width="30%"
      :visible.sync="ruleForm.visible"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="标签名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm">确定</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { websock } from "@/websock";
import { Message } from "element-ui";

export default {
  data() {
    return {
      rules: {
        name: [{ required: true, message: "请输入标签名称", trigger: "blur" }],
      },
      list: [],
      ruleForm: {
        visible: false,
        name: "",
        id: null,
      },
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    // 获取数据
    fetchData() {
      websock
        .fileTagList()
        .then((data) => {
          this.list = data;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 点击新增
    onclickAdd() {
      this.ruleForm.visible = true;
      this.ruleForm.name = "";
    },
    // 修改
    onClickChange(row) {
      this.ruleForm.name = row.name;
      this.ruleForm.id = row.id;
      this.ruleForm.visible = true;
    },
    // 点击增加
    successFun() {
      this.ruleForm.visible = false;
      this.ruleForm.id = null;
      this.ruleForm.name = "";
      this.fetchData();
      return Message({
        message: "操作成功",
        type: "success",
        duration: 3 * 1000,
      });
    },
    submitForm() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let params = {
            name: this.ruleForm.name,
          };
          let data;

          if (this.ruleForm.id != null) {
            params.id = this.ruleForm.id;
            data = await websock.fileTagModify(params);
          } else {
            if (params.id) delete params.id;
            data = await websock.fileTagAdd(params);
          }
          if (data.code == 0) return this.successFun();
        }
      });
    },
    resetForm() {
      this.ruleForm.name = "";
      this.ruleForm.id = null;
      this.ruleForm.visible = false;
      this.$refs.ruleForm.resetFields();
    },
  },
};
</script>
